package com.black.knight.chess.calls;

import org.restlet.resource.ClientResource;

import com.black.knight.chess.R;
import com.black.knight.chess.SahMatActivity;
import com.black.knight.chess.common.Base;
import com.black.knight.chess.common.Game;
import com.black.knight.chess.common.GameResource;
import com.black.knight.chess.model.SettingsModel;
import com.black.knight.chess.utils.Utils;

public class PublishGameCall implements Runnable {

	private String category;
	
	private String fen;	

	private String png;
	
	private String description;
	
	private SahMatActivity context;
	
	private Base base = null;
	
	public PublishGameCall(SahMatActivity context) {
		this.context = context;
	}
	
	@Override
	public void run() {
		if(SettingsModel.getInstance().getLoggedUser() == null) return;
		ClientResource cr = new ClientResource(SettingsModel.SERVICE_URL + "/games/New");
		
		cr.setRequestEntityBuffering(true);
		
		GameResource resource = cr.wrap(GameResource.class);
		
		try {
			Game game = new Game();
			game.setCategory(category);
			game.setDescription(description);
			game.setFen(fen);
			game.setPgn(png);
			
			base = resource.saveGame(game);
		} catch (Exception e) {
			base = new Base(false, "");
		}
		
		if(base.getSuccess()) {
			context.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					try {
						Utils.displayToastMessage(context, context.getResources().getString(R.string.game_published_successfully));
					} catch (Exception e) { }
				}
			});
		} else {
			context.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					try {						
						Utils.displayToastMessage(context, context.getResources().getString(R.string.unable_to_publish_game));
					} catch (Exception e) { }
				}
			});
		}
	}
	
	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getFen() {
		return fen;
	}

	public void setFen(String fen) {
		this.fen = fen;
	}

	public String getPng() {
		return png;
	}

	public void setPng(String png) {
		this.png = png;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
}
